package org.example.day20240909.dao;

import org.example.day20240909.model.Account;
import org.example.day20240909.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountDao {
    public Account checkLogin(Account account) {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from account where account = ? and password = ?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, account.getAccount());
            ps.setString(2, account.getPassword());
            rs = ps.executeQuery();

            while (rs.next()) {
                Account accountReturn = new Account();
                accountReturn.setUser_id(rs.getString("employee_id"));
                accountReturn.setAccount(rs.getString("account"));
                accountReturn.setPassword(rs.getString("password"));
                accountReturn.setRole(rs.getString("role"));
                return accountReturn;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
